-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(insights): introduce insights
middleware (1/4)
#4446
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 5067ec7:
|
053783e
to
467a648
Compare
Co-authored-by: Haroen Viaene <[email protected]>
2386a91
to
3ab96a2
Compare
Co-authored-by: Haroen Viaene <[email protected]>
src/lib/main.ts
Outdated
@@ -39,5 +40,6 @@ instantsearch.createInfiniteHitsSessionStorageCache = createInfiniteHitsSessionS | |||
instantsearch.highlight = helpers.highlight; | |||
instantsearch.snippet = helpers.snippet; | |||
instantsearch.insights = helpers.insights; | |||
instantsearch.middleware = middleware; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
middleware vs middlewares
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"middlewares" is not proper English.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://en.wiktionary.org/wiki/middleware
I don't think I've seen s
at the end of middleware
before, though.
So you agree it's natural to have middleware
although we have routers, stateMappings, connectors, widgets
, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be middlewares, at least for the object containing all possible middlewares. Interestingly my spell-checker only accepts singular "middleware" as correct, and not "middlewares"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://www.wordhippo.com/what-is/the-plural-of/middleware.html
While middleware
is more common, it's also safe to use middlewares
if we want to explicitly indicate it's a collection.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-authored-by: François Chalifour <[email protected]>
insights
middleware
insights
middleware (1/3)insights
middleware (1/n)
src/middleware/insights.ts
Outdated
import { warning, noop } from '../lib/utils'; | ||
|
||
export type InsightsProps = { | ||
insightsClient: false | InsightsClient; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accepting false
for the Insights client is confusing because it can't be true
. Why don't we use null
as we're doing with other options?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
null is a fair option, I suggested false
, since that's what I did with Vue: https://github.com/algolia/vue-instantsearch/blob/master/src/mixins/widget.js#L47-L55
EDIT: it's actually true
there, but null
would make more sense (maybe a next major)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, null
sounds good.
b0b7add
Co-authored-by: François Chalifour <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm if open comments are resolved 👍
src/middleware/insights.ts
Outdated
}; | ||
}; | ||
|
||
function getAppIdAndApiKey(searchClient) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add a unit test here. Since we run the tests with algoliasearch v3 and v4 separately, you should be able to cover both branches.
Other case we can add a dev dependency for "algoliasearch-v3": "npm:algoliasearch@3"
and import algoliasearch from 'algoliasearc-v3'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL: "npm:algoliasearch@3" syntax. Nice! 7f49f25
instantSearchInstance: InstantSearch, | ||
}) => MiddlewareDefinition; | ||
|
||
export { createRouter, RouterProps } from './createRouter'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not sure if anyone uses this, but this changes the API
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Co-authored-by: Haroen Viaene <[email protected]>
Co-authored-by: Haroen Viaene <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's good now, assuming that nobody imported instantsearch.js/es/middleware
, since that will be broken now. Since middleware is experimental, I think it's ok
src/middlewares/index.ts
Outdated
@@ -0,0 +1,2 @@ | |||
export { createInsightsMiddleware } from './insights'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be consistent with the file naming, we can rename the file createInsightsMiddleware
(and perhaps createRouterMiddleware
?).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
183b1ca good call!
src/middlewares/index.ts
Outdated
@@ -0,0 +1,2 @@ | |||
export { createInsightsMiddleware } from './insights'; | |||
export * from './createRouter'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We exports the types from the router middleware. Any opinions on aligning the two? (either exporting the types or not exporting the types)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's good to export all of them e7c7e58
insights
middleware (1/n)insights
middleware (1/4)
* chore: dummy commit * add sendEvent method * send event from refinementList * accept custom payload at sendEvent * delegate to onEvent or actually send event * store _insightsMiddleware in instantSearchInstance * add clickedFilters to InsightsClientMethod * rename $$type of middlewares * chore: remove ! * add sendEvent for hits * update for consistency * send view event when rendering hits * expose bindEvent to templates * handle insightsEvent from templates * allow single hit for bind/sendEventForHits * assign sendEventToInsights by the middleware * Update src/lib/insights/listener.tsx Co-authored-by: Haroen Viaene <[email protected]> * require eventName for click and conversion in hits * fix type errors * fix test cases * reuse $$type from connectors * remove $$type from middleware * Apply suggestions from code review Co-authored-by: François Chalifour <[email protected]> * fix: allow only one way for sending events * feat(insights): implement sendEvent in all the connectors (3/4) (#4463) * update infiniteHits * modify createSendEventForHits to accept index instead of helper * add sendEvent to connectAutocomplete * add sendEvent to connectGeoSearch * add sendEvent to connectHierarchicalMenu * add sendEvent to connectBreadcrumb * add sendEvent to connectMenu * add sendEvent to connectNumericMenu * extract getRefinedState in connectRange * add sendEvent to connectRange * add sendEvent to connectRatingMenu * add sendEvent to connectToggleRefinement * fix test error * fix export * fix type error * use $$type instead of hard-code * remove sendEvent from connectBreadcrumb * moved createSendEvent to the top level in the files * feat(insights): add tests (4/4) (#4464) * add tests for userToken * fix import paths * fix test cases to accept null instead of false as insightsClient * update bundlesize * fix lint errors * test sendEventToInsights * use $$type instead of hard-code * remove sendEvent from connectBreadcrumb * moved createSendEvent to the top level in the files * log insights event from storybook * add test for connectors * add tests for createSendEvent helpers * add integration tests for hits and infinite-hits widgets with bindEvent in templates * update bundlesize * Update src/connectors/hits/__tests__/connectHits-test.ts Co-authored-by: Haroen Viaene <[email protected]> * use factory function instead of globally exposed variables * clean up * update comment * use runAllMicroTasks instead of nextTick * fix: type errors * fix wrong import paths * fix: pass insightsClient to onEvent as the second parameter * update titles of describe blocks Co-authored-by: Haroen Viaene <[email protected]> Co-authored-by: Haroen Viaene <[email protected]> Co-authored-by: Haroen Viaene <[email protected]> Co-authored-by: François Chalifour <[email protected]>
Summary
This PR introduces
insights
middleware.It is to automatically setup userToken across search calls and Insights api calls.
Once this middleware is added to
instantSearchInstance
, when user callsthe middleware will automatically apply the userToken to the searchParameters. Then the next search call will include this userToken.
Stacked PRs
insights
middleware ← currentexamples
insightsClient: false
: https://codesandbox.io/s/instantsearchjs-1igbb?file=/src/app.js(working example: esm, umd)